package com.zbvc.web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zbvc.dao.BookDao;
import com.zbvc.dao.impl.BookDaoImpl;
import com.zbvc.entity.Book;

/**
 * Servlet implementation class UpdateBookServlet
 */
@WebServlet(name = "updateBook", urlPatterns = { "/updateBook" })
public class UpdateBookServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateBookServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		
		
		//获取需要修改的字段值
		int bid = Integer.parseInt(request.getParameter("bid"));
		String name = request.getParameter("name");
		String autho = request.getParameter("autho");
		String press = request.getParameter("press");
		int num = Integer.parseInt(request.getParameter("num"));
		Book book = new Book();
		book.setBid(bid); // bid
		book.setNum(num);
		book.setPress(press);
		book.setName(name);
		book.setAuthor(autho);
		// 调用Dao层的updateBookById(Book book)方法进行更新操作
		BookDao bookDao = new BookDaoImpl();
		int row = bookDao.updateBookById(book);
		System.out.println("更新"+row+"条"); // 输出个日志便于调错
		//跳转到admin_book.jsp页面
		response.sendRedirect("/bookManager/admin/admin_book.jsp");
		
	}

}
